import requests
import json
import schedule
import time
import datetime


def get_opentoken():#获取公众号token
	appid=''#公众号appid
	secret=''#公众号密码
	url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}".format(appid,secret)
	res = requests.get(url)
	j = json.loads(res.text)
	return j['access_token']

def get_users():#获取所有公众号关注用户
	access_token = get_opentoken()
	url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token={}'.format(access_token)
	res = requests.get(url)
	j = json.loads(res.text)
	print(j)

def get_user():#获取公众号单个关注用户
	access_token = get_opentoken()
	openid=''#用户openid
	url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token={}&openid={}&lang=zh_CN'.format(access_token,openid)
	res = requests.get(url)
	j = json.loads(res.text)
	print(j)

def postmsg():#发送公众号模版消息
	access_token = get_opentoken()
	push_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token)	
	params={
				"touser":"",#用户openid
				"template_id":"MdmKL8PqSsmuOizBgxo6T7JNzFecHfK4tQYHiNA2WhA",
				"url":"https://weiphp.loaol.com/index.php?s=/w16/home/Index/leaflets.html",
				"topcolor":"#FF0000",
				"data":{
					"first": {
					"value":"您好，您的订单包裹已被您拒收",
					"color":"#173177"
					},
					"order_id":{
					"value":"C0-xxxxxxx-xxxxxxx",
					"color":"#173177"
					},
					"package_id":{
					"value":"xxxxxxxxxxxxxx",
					"color":"#173177"
					},
					"remark":{
					"value":"￥125.67 现金付款",
					"color":"#173177"
					}
				}
			}
	j = json.dumps(params)		
	r = requests.post(push_url, data=j)
	return r.text			

# schedule.every(1).minutes.do(requests_post,'minutes-') #时间任务

# while True:   #时间任务
#     schedule.run_pending()
#     time.sleep(1)

if __name__ == '__main__':
	# token=get_token()
	# print("token:"+token)
	rs=postmsg()
	print(rs)
	# get_users()
	# get_user()